package com.yunbiao.ybbusstopplate.utils.core.power;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * 京东方画屏-定时开关机
 * Created by Administrator on 2019/4/26.
 */

public class JDFPowerControl {
    private static final String TAG="JDFPowerControl";
    private JDFPowerControl(){}
    public static JDFPowerControl powerControl;

    public static JDFPowerControl getInstance(){
        if (powerControl==null){
            powerControl=new JDFPowerControl();
        }
        return powerControl;
    }

    //开启定时开关机任务
    public void powerOnOffTool(){
        Calendar[] powerOnOffArray = getPowerOnOffArray();
        if (powerOnOffArray.length<=0){
            return;
        }
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmm");
        String poweroff = dateFormat.format(powerOnOffArray[0].getTime());
        String poweron = dateFormat.format(powerOnOffArray[1].getTime());
        Log.d(TAG,"定时开关机时间：on-"+poweron+",off-"+poweroff);
        standby(1,poweroff);
        wakeup(1,poweron);
    }

    //待机
    private void standby(int type, String time){
        //setprop persist.sys.standby 1201609191805  2016年09月21日18点05分待机  1.开启 0.关闭
        String shell_code="setprop persist.sys.standby "+type+time;
        execmd_shell(shell_code);
    }
    //唤醒
    private void wakeup(int type, String time){
        //setprop persist.sys.wakeup 1201609191805  2016年09月21日18点05分唤醒  1.开启 0.关闭
        String shell_code="setprop persist.sys.wakeup "+type+time;
        execmd_shell(shell_code);
    }


    //执行shell命令
    private void execmd_shell(String shell_code){
        try {
            Process process= Runtime.getRuntime().exec("sh");
            DataOutputStream dos=new DataOutputStream(process.getOutputStream());
            dos.writeBytes(shell_code);// 2018年09月19日18点05分待机
            dos.flush();
            dos.close();
            DataInputStream dis=new DataInputStream(process.getInputStream());
            String standby=dis.readLine();
            dis.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }


    private Calendar[] getPowerOnOffArray() {
        Calendar[] calendars=new Calendar[2];
        Long[] powerOn = PowerOffTool.getPowerOffTool().getPowerTime(PowerOffTool.POWER_ON);
        Long[] powerOff = PowerOffTool.getPowerOffTool().getPowerTime(PowerOffTool.POWER_OFF);
        if (powerOn != null && powerOff != null) {
            Long offh = powerOff[0] * 24 + powerOff[1];
            Long offm = powerOff[2];
            Long onh = powerOn[0] * 24 + powerOn[1];
            Long onm = powerOn[2];

            Calendar powerOffDate = Calendar.getInstance();
            powerOffDate.add(Calendar.MINUTE, (int) (offh * 60 + offm));
            Calendar powerOnDate = Calendar.getInstance();
            if ((onh * 60 + onm) > (offh * 60 + offm)) {
                long offset = (onh * 60 + onm) - (offh * 60 + offm);
                onm = offset % 60;
                onh = offset / 60;
                powerOnDate.add(Calendar.MINUTE, (int) (offh * 60 + offm) + (int) (onh * 60 + onm));
            } else {
                powerOnDate.add(Calendar.MINUTE, (24 * 60) + (int) (onh * 60 + onm));
            }
            Log.e(TAG, "getPowerContrArray: " + " setPowerOffDate:   " + powerOffDate.getTime().toLocaleString() + "   setPowerOnDate:   " + powerOnDate.getTime().toLocaleString());
            calendars[0]=powerOffDate;
            calendars[1]=powerOnDate;
        }
        return calendars;
    }
}
